package com.ruoyi.quartz.task;


import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.service.ISysJobService;
import com.snowRemovingUser.domain.TPrediction;
import com.snowRemovingUser.dto.OriginVo;
import com.snowRemovingUser.dto.PredictionVo;
import com.snowRemovingUser.service.ITPredictionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;
import java.util.Map;

@Component("forecastImageTask")
public class ForecastImageTask {


    /** log */
    protected Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private ITPredictionService predictionService;

    @Autowired
    private ISysJobService sysJobService;




    public void start() throws Exception{



        // 检查预报是否已经发布
        TPrediction tPrediction = new TPrediction();
        tPrediction.setParentId(0L);
        List<TPrediction> tPredictions = predictionService.selectTPredictionList(tPrediction);

        Integer predictionStatus = tPredictions.get(0).getPredictionStatus();



        if (predictionStatus == 0){

            Map<String, Object> dataMap = predictionService.latestInfo("YAXI");

            // 标题
            Object roadName = dataMap.get("roadName");
            // 基础数据
            List<PredictionVo> predictionVo = (List<PredictionVo>)dataMap.get("predictionVo");


            // 第一个数据
            PredictionVo predictionVo1 = predictionVo.get(0);
            PredictionVo predictionVo2 = predictionVo.get(1);

            File imageFile = new File("/data/dataCenter/app/template/summer.png");
            // 将图片文件 转成 BufferedImage格式
            BufferedImage image = ImageIO.read(imageFile);
            // 在图像上 输出文字
            Graphics2D graphics = (Graphics2D) image.getGraphics();


            // 开启抗锯齿(提高字体的清晰度)
            graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

            // 1. 24小时气象概要
            Color customColor = new Color(232, 236, 238);  // 设置文字颜色
            graphics.setColor(customColor);
            graphics.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 18)); // 设置文字字体和大小
            int textX = 110;
            int textY = 312;
            graphics.drawString(predictionVo1.getComment(), textX, textY);
            graphics.drawString(predictionVo2.getComment(), textX, textY+ 35);


            // 2. 精细化预报
            int text2X = 128;
            int text2Y = 530;
            graphics.drawString(predictionVo1.getPredictionTable().getWeatherExplain(), text2X, text2Y);
            graphics.drawString(predictionVo2.getPredictionTable().getWeatherExplain(), text2X, text2Y+ 270);

            // 3. 未来3天趋势
            int text3X = 128;
            int text3Y = 1235;
            graphics.drawString(predictionVo1.getPredictionComment(), text3X, text3Y);
            graphics.drawString(predictionVo1.getPredictionComment(), text3X, text3Y + 35);


            // 4. 图表
            List<String> transTitles = predictionVo1.getPredictionTable().getTransTitle();
            List<String> transTitles2 = predictionVo2.getPredictionTable().getTransTitle();



            Color transTitleColor = new Color(181, 90, 110);  // 设置文字颜色
            graphics.setColor(transTitleColor);
            Font font = new Font("宋体", Font.BOLD, 14);
            graphics.setFont(font); // 设置文字字体和大小

            // (1) 横向标题
            int titleX = 0;
            int titleY = 0;
            for (int i = 0; i < transTitles.size(); i++) {

                if (i ==0){
                    titleX = 160;
                    titleY = 590;
                    graphics.drawString(transTitles.get(i), titleX, titleY);
                }else if (i ==1){

                    titleX = titleX + 95;
                    graphics.drawString(transTitles.get(i), titleX, titleY);
                }else {

                    titleX = titleX + 60;
                    graphics.drawString(transTitles.get(i), titleX, titleY);
                }
            }

            titleX = 0;
            titleY = 0;
            for (int i = 0; i < transTitles2.size(); i++) {

                if (i ==0){
                    titleX = 160;
                    titleY = 857;
                    graphics.drawString(transTitles2.get(i), titleX, titleY);
                }else if (i ==1){

                    titleX = titleX + 95;
                    graphics.drawString(transTitles2.get(i), titleX, titleY);
                }else {

                    titleX = titleX + 60;
                    graphics.drawString(transTitles2.get(i), titleX, titleY);
                }
            }








            // (2) 纵向标题
            List<String> originTitles = predictionVo1.getPredictionTable().getOriginTitle();
            List<String> originTitles2 = predictionVo2.getPredictionTable().getOriginTitle();


            int originTitleX = 0;
            int originTitleY = 0;


            for (int i = 0; i < originTitles.size(); i++) {

                if (i == 0){
                    originTitleX = originTitleX + 160;
                    originTitleY = originTitleY + 635;
                    graphics.drawString(originTitles.get(i), originTitleX, originTitleY);
                    originTitleX = originTitleX - 10;
                }else {
                    originTitleY = originTitleY + 43;
                    graphics.drawString(originTitles.get(i), originTitleX, originTitleY);
                }
            }

            originTitleX = 0;
            originTitleY = 0;
            for (int i = 0; i < originTitles2.size(); i++) {

                if (i == 0){
                    originTitleX = originTitleX + 160;
                    originTitleY = originTitleY + 900;
                    graphics.drawString(originTitles2.get(i), originTitleX, originTitleY);
                    originTitleX = originTitleX - 10;
                }else {
                    originTitleY = originTitleY + 43;
                    graphics.drawString(originTitles2.get(i), originTitleX, originTitleY);
                }
            }




            // (3) 数据内容

            List<OriginVo> originData = predictionVo1.getPredictionTable().getOriginData();
            List<OriginVo> originData2 = predictionVo2.getPredictionTable().getOriginData();


            int originDataX = 270;
            int originData1Y = 630;
            int originData2Y = 675;
            int originData3Y = 715;

            for (OriginVo originDatum : originData) {


                FontMetrics fontMetrics = graphics.getFontMetrics(font);

                // 计算文字的宽度和高度
                int rainWidth = fontMetrics.stringWidth(originDatum.getRain());
                int rainHeight = fontMetrics.getHeight();
                // 计算在指定坐标上居中显示文字的起始坐标
                int rainX = originDataX - rainWidth / 2;
                int rainY = originData1Y - rainHeight / 2 + fontMetrics.getAscent();

                graphics.drawString(originDatum.getRain(), rainX, rainY);



                // 计算文字的宽度和高度
                int tmp1Width = fontMetrics.stringWidth(originDatum.getTmp1());
                int tmp1Height = fontMetrics.getHeight();
                // 计算在指定坐标上居中显示文字的起始坐标
                int tmp1X = originDataX - tmp1Width / 2;
                int tmp1Y = originData2Y - tmp1Height / 2 + fontMetrics.getAscent();
                graphics.drawString(originDatum.getTmp1(), tmp1X, tmp1Y);


                // 计算文字的宽度和高度
                int tmp2Width = fontMetrics.stringWidth(originDatum.getTmp2());
                int tmp2Height = fontMetrics.getHeight();
                // 计算在指定坐标上居中显示文字的起始坐标
                int tmp2X = originDataX - tmp2Width / 2;
                int tmp2Y = originData3Y - tmp2Height / 2 + fontMetrics.getAscent();



                graphics.drawString(originDatum.getTmp2(), tmp2X, tmp2Y);
                originDataX = originDataX +60;
            }


            originDataX = 270;
            originData1Y = 893;
            originData2Y = 938;
            originData3Y = 978;

            for (OriginVo originDatum : originData2) {


                FontMetrics fontMetrics = graphics.getFontMetrics(font);

                // 计算文字的宽度和高度
                int rainWidth = fontMetrics.stringWidth(originDatum.getRain());
                int rainHeight = fontMetrics.getHeight();
                // 计算在指定坐标上居中显示文字的起始坐标
                int rainX = originDataX - rainWidth / 2;
                int rainY = originData1Y - rainHeight / 2 + fontMetrics.getAscent();

                graphics.drawString(originDatum.getRain(), rainX, rainY);



                // 计算文字的宽度和高度
                int tmp1Width = fontMetrics.stringWidth(originDatum.getTmp1());
                int tmp1Height = fontMetrics.getHeight();
                // 计算在指定坐标上居中显示文字的起始坐标
                int tmp1X = originDataX - tmp1Width / 2;
                int tmp1Y = originData2Y - tmp1Height / 2 + fontMetrics.getAscent();
                graphics.drawString(originDatum.getTmp1(), tmp1X, tmp1Y);


                // 计算文字的宽度和高度
                int tmp2Width = fontMetrics.stringWidth(originDatum.getTmp2());
                int tmp2Height = fontMetrics.getHeight();
                // 计算在指定坐标上居中显示文字的起始坐标
                int tmp2X = originDataX - tmp2Width / 2;
                int tmp2Y = originData3Y - tmp2Height / 2 + fontMetrics.getAscent();
                graphics.drawString(originDatum.getTmp2(), tmp2X, tmp2Y);
                originDataX = originDataX + 60;
            }


            // 关闭抗锯齿
            graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);


            String imgName = (String) dataMap.get("road");
            // 保存绘制后的图像
            File outputImageFile = new File("/data/dataCenter/app/prediction_img/" + imgName + ".png");
            assert image != null;
            ImageIO.write(image, "png", outputImageFile);

            SysJob sysJob = new SysJob();
            sysJob.setJobId(106L);
            sysJobService.run(sysJob);


            // 修改状态
            for (TPrediction prediction : tPredictions) {

                prediction.setPredictionStatus(1);
                predictionService.updateTPrediction(prediction);
            }


            logger.info("预测报表成功发布");
        } else {


            logger.info("预测报表已发布，无需再次发布");
        }




    }



















}
